<?php
namespace app\admin\controller;

use \think\Cache;
use \think\Controller;
use think\Loader;
use think\Db;
use \think\Cookie;
use app\admin\controller\User;
use app\admin\model\Product as articleModel;
use app\admin\model\ProductCate as cateModel;
class Product extends Controller
{
    public function index()
    {
        $model = new articleModel();
        $post = $this->request->post();
        if (isset($post['title']) and !empty($post['title'])) {
            $where['title'] = ['like', '%' . $post['title'] . '%'];
        }
        if (isset($post['product_cate_id']) and $post['product_cate_id'] > 0) {
            $where['product_cate_id'] = $post['product_cate_id'];
        }

        if (isset($post['admin_id']) and $post['admin_id'] > 0) {
            $where['admin_id'] = $post['admin_id'];
        }
        $cate = $model->select();
        $cates = $model->catelist($cate);
        //添加最后修改人的name
        $this->assign('product',$cates);
        $info['cate'] = Db::name('news_cate')->select();
        //dump($info['cate']);die();
//        foreach ($info['cate'] as $v){
//            $v['cate']=mb_substr($v['cate'],0,6)."......";
//        }
        $info['admin'] = Db::name('admin')->select();
        // dump($info);die();
        $this->assign('info',$info);
        return $this->fetch();
    }



    public function publish()
    {
        //获取菜单id
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $model = new articleModel();
        $cateModel = new cateModel();
        //是正常添加操作
        if($id > 0) {
            //是修改操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['title', 'require', '标题不能为空'],
                    ['product_cate_id', 'require', '请选择分类'],
                    ['thumb', 'require', '请上传缩略图'],
                    ['description', 'require', '文章内容不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                //验证菜单是否存在
                $article = $model->where('id',$id)->find();
                if(empty($article)) {
                    return $this->error('id不正确');
                }
                //设置修改人
                $post['edit_admin_id'] = Cookie::get('admin');
                if(false !== $model->allowField(true)->save($post,['id'=>$id])) {
                    return $this->success('修改成功','admin/product/index');
                } else {
                    //addlog($model->id);//写入日志
                    return $this->error('修改失败');
                }
            } else {
                //非提交操作
                $article = $model->where('id',$id)->find();
                $cates = $cateModel->select();
                $cates_all = $cateModel->catelist($cates);
                $this->assign('cates',$cates_all);
                if(!empty($article)) {
                    $this->assign('article',$article);
                    return $this->fetch();
                } else {
                    return $this->error('id不正确');
                }
            }
        } else {
            //是新增操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['title', 'require', '标题不能为空'],
                    ['product_cate_id', 'require', '请选择分类'],
                    ['thumb', 'require', '请上传缩略图'],
                    ['description', 'require', '文章内容不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                //设置创建人
                $post['admin_id'] = Cookie::get('admin');
                //设置修改人
                $post['edit_admin_id'] = $post['admin_id'];
                if(false == $model->allowField(true)->save($post)) {
                    return $this->error('添加失败');
                } else {
                    //addlog($model->id);//写入日志
                    return $this->success('添加成功','admin/product/index');
                }
            } else {
                //非提交操作
                $cate = $cateModel->select();
                $cates = $cateModel->catelist($cate);
                $this->assign('cates',$cates);
                return $this->fetch();
            }
        }

    }



    public function delete()
    {
        if($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false == Db::name('product')->where('id',$id)->delete()) {
                return $this->error('删除失败');
            } else {
                //addlog($id);//写入日志
                return $this->success('删除成功','admin/product/index');
            }
        }
    }


    public function is_top()
    {
        if($this->request->isPost()){
            $post = $this->request->post();
            if(false == Db::name('news')->where('id',$post['id'])->update(['is_top'=>$post['is_top']])) {
                return $this->error('设置失败');
            } else {
                //addlog($post['id']);//写入日志
                return $this->success('设置成功','admin/product/index');
            }
        }
    }


    public function status()
    {
        if($this->request->isPost()){
            $post = $this->request->post();
            if(false == Db::name('product')->where('id',$post['id'])->update(['status'=>$post['status']])) {
                return $this->error('设置失败');
            } else {
                //addlog($post['id']);//写入日志
                return $this->success('设置成功','admin/product/index');
            }
        }
    }

}
